﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Management_MedicalDirector_Danhsachyeucau : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void view_click(object sender,EventArgs e) {
        String id = (sender as LinkButton).CommandArgument;
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT tbStaff.IDWard,tbWard.Name,tbStaff.FirstName,tbStaff.LastName,RequisitionDate from tbStaff,tbWard,tbRequisition where tbStaff.IDWard=tbWard.ID and tbRequisition.IDStaff=tbStaff.ID and tbRequisition.ID="+id;
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            lbNumber.Text ="Ward"+ reader.GetInt32(0);
            lbName.Text = reader.GetString(1);
            lbReTo.Text = reader.GetString(2)+" "+reader.GetString(3);
            lbReDate.Text = reader.GetDateTime(4).ToString() ;
        }
        //--------------------------------------------------
        pnDetails.Visible = true;
        lbTitle.Text="Chi tiết yêu cầu";
        lbRequisition.Text = "000" + id;
        detail.SelectParameters["id"].DefaultValue = id;
        GridView3.DataBind();
        if (Int32.Parse(dlSelect.SelectedValue) == 3)
        {
            btAccept.Text = "Đã Nhận";
            btCancel.Visible = false;
        }
        else {
            btAccept.Text = "Chấp Nhận";
            btCancel.Visible = true;
        }

    }
    protected void accept_click(object sender, EventArgs e) {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        String id = lbRequisition.Text.Substring(3);
        string sql = "Update tbRequisition set Accepted=@accept where id=@id";
        SqlCommand cmd = new SqlCommand(sql, con);
        if(btCancel.Visible)
            cmd.Parameters.AddWithValue("@accept",3);
        else
            cmd.Parameters.AddWithValue("@accept", 4);
        cmd.Parameters.AddWithValue("@id",id);
        cmd.ExecuteNonQuery();
        yeucau.SelectParameters["Accepted"].DefaultValue = dlSelect.SelectedValue;
        GridView2.DataBind();
        con.Close();
        con.Open();
        if (!btCancel.Visible) {
            int[,] thuoc=new int[10,2];
            sql = "Select IDDrug,Quantity from tbRequisitionDetail where IDRequisition="+id;
            cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            int i=0;
            while (reader.Read()) {
                thuoc[i, 0] = reader.GetInt32(0);
                thuoc[i, 1] = reader.GetInt32(1);
                i++;
            }
            con.Close();
            for (int j = 0; j < i; j++)
            {
                con.Open();
                sql = "Update tbTotalDrug set Quantity=Quantity-@Quantity where IDDrug=@id";
                cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Quantity", thuoc[j,1]);
                cmd.Parameters.AddWithValue("@id",thuoc[j,0]);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    protected void cancel_click(object sender, EventArgs e) {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        String id = lbRequisition.Text.Substring(3);
        string sql = "Update tbRequisition set Accepted=@accept where id=@id";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.AddWithValue("@accept", 2);
        cmd.Parameters.AddWithValue("@id", id);
        cmd.ExecuteNonQuery();
        yeucau.SelectParameters["Accepted"].DefaultValue = dlSelect.SelectedValue;
        GridView2.DataBind();
    }
    protected void dlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        yeucau.SelectParameters["Accepted"].DefaultValue = dlSelect.SelectedValue;
        GridView2.DataBind();
        pnDetails.Visible = false;

    }
}